package com.dongan.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dongan.mapper.DesignProcedureMapper;
import com.dongan.pojo.*;
import com.dongan.mapper.CodesMapper;
import com.dongan.service.CodesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongan.service.DesignProcedureDetailsService;
import com.dongan.service.DesignProcedureService;
import com.dongan.service.FileService;
import com.dongan.utils.GetSysDateTime;
import com.dongan.utils.NumberFormatUtil;
import com.dongan.utils.SerialNumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 设计过程表 服务实现类
 * </p>
 *
 * @author longTao
 * @since 2021-06-15
 */
@Service
public class DesignProcedureServiceImpl extends ServiceImpl<DesignProcedureMapper, DesignProcedure> implements DesignProcedureService {

    @Autowired
    private DesignProcedureMapper mapper;
    @Autowired
    private DesignProcedureDetailsService detailsService;
    @Autowired
    private FileService fileService;
    @Autowired
    private CodesService codesService;
    /**
     * 添加工序设计
     *
     * @param procedure
     * @param details
     */
    @Override
    public boolean insertProcedure(DesignProcedure procedure, List<DesignProcedureDetails> details) {
        String dId = 201 + NumberFormatUtil.numberFormat2(4, SerialNumber.designProcedureSN);
        procedure.setDesignId(dId);
        //REGISTER 登记人
        Users user= (Users) SerialNumber.user;
        procedure.setRegister(user.getLoginId());
        //REGISTER_TIME 登记时间
        procedure.setRegisterTime(GetSysDateTime.getDatetime());
        //DESIGNER 设计人
        procedure.setDesigner(user.getLoginId());
        //CHECK_TAG S001-0: 等待审核
        procedure.setCheckTag("S001-0");
        //变更标签
        procedure.setChangeTag("B002-0");
        //DESIGN_MODULE_TAG	 G002-0: 未设计	工序物料设计标志
        procedure.setDesignModuleTag("G002-0");
        //DESIGN_MODULE_CHANGE_TAG	G003-0: 未变更	工序物料变更标志
        procedure.setDesignModuleChangeTag("G003-0");
        BigDecimal sum=new BigDecimal(0);
        BigDecimal dNumber=new BigDecimal(1);
        for (DesignProcedureDetails detail : details) {
            detail.setSubtotal(detail.getCostPrice().multiply(detail.getLabourHourAmount()));
            //LABOUR_HOUR_AMOUNT 工时数 SUBTOTAL 工时成本小计
            sum=sum.add(detail.getSubtotal());
            detail.setDetailsNumber(dNumber);
            dNumber=dNumber.add(new BigDecimal(1));
            //DESIGN_MODULE_TAG	D002-0: 未设计	当前工序物料标志
            detail.setDesignModuleTag("D002-0");
            //DESIGN_MODULE_CHANGE_TAG	D003-0: 未变更	当前工序物料变更标志
            detail.setDesignModuleChangeTag("D003-0");
            //REGISTER	VARCHAR2(50)	登记人
            detail.setRegister(user.getLoginId());
            //REGISTER_TIME	DATE	登记时间
            detail.setRegisterTime(GetSysDateTime.getDatetime());
        }
        //COST_PRICE_SUM工时总成本
        procedure.setCostPriceSum(sum);
        int row=mapper.insert(procedure);
        if(row>0){
            SerialNumber.designProcedureSN=SerialNumber.designProcedureSN+1;
            Codes codes=new Codes();
            codes.setName("designProcedureSN");
            codes.setDefValue(Integer.valueOf(SerialNumber.designProcedureSN).toString());
            codesService.updateById(codes);
            for (DesignProcedureDetails detail : details) {
                detail.setParentId(procedure.getId());
            }
            boolean flag=detailsService.saveBatch(details);
            if(flag){
                //修改商品物料设计表
                File file = new File();
                file.setDesignProcedureTag("G001-1");
                UpdateWrapper<File> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("PRODUCT_ID", procedure.getProductId());
                return fileService.update(file, updateWrapper);
            }
        }
        return false;
     }

    /**
     * 修改工序设计
     *
     * @param procedure
     * @param details
     * @return
     */
    @Override
    public boolean updateProcedure(DesignProcedure procedure, List<DesignProcedureDetails> details) {
        //CHECK_TAG	S001-0: 等待审核	审核标志
        procedure.setCheckTag("S001-0");
        //CHANGER	VARCHAR2(50)	变更人
        Users user= (Users) SerialNumber.user;
        procedure.setChanger(user.getLoginId());
        //CHANGE_TIME	DATE	变更时间
        procedure.setChangeTime(GetSysDateTime.getDatetime());
        //CHANGE_TAG	B002-1: 已变更	变更标志
        procedure.setChangeTag("B002-1");
        BigDecimal sum=new BigDecimal(0);
        BigDecimal dNumber=new BigDecimal(1);
        for (DesignProcedureDetails detail : details) {
            //LABOUR_HOUR_AMOUNT 工时数 SUBTOTAL 工时成本小计
            detail.setSubtotal(detail.getLabourHourAmount().multiply(detail.getCostPrice()));
            sum=sum.add(detail.getSubtotal());
            detail.setDetailsNumber(dNumber);
            dNumber=dNumber.add(new BigDecimal(1));
            //DESIGN_MODULE_TAG	D002-0: 未设计	当前工序物料标志
            detail.setDesignModuleTag("D002-0");
            //DESIGN_MODULE_CHANGE_TAG	D003-0: 未变更	当前工序物料变更标志
            detail.setDesignModuleChangeTag("D003-0");
            detail.setParentId(procedure.getId());
        }
        //COST_PRICE_SUM工时总成本
        procedure.setCostPriceSum(sum);
        int row=mapper.updateById(procedure);
        if(row>0){
            UpdateWrapper<DesignProcedureDetails> wrapper=
                    new UpdateWrapper<DesignProcedureDetails>();
            wrapper.eq("PARENT_ID",procedure.getId());
            boolean flag=detailsService.remove(wrapper);
                boolean flag2=detailsService.saveBatch(details);
                return flag2;
        }
        return false;
    }
}
